<!--
 * @Author: gao_m3
 * @Date: 2020-12-01 08:57:59
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-12-29 19:28:29
 * @Descripttion: 
-->
<template>
  <div>
    <fks-button
      v-if="serverType != 'fileServerConfig'"
      v-permission:1="['mail:add']"
      icon="fks-icon-plus"
      type="primary"
      style="z-index: 1; float: right; margin-top: 1px; margin-bottom: 16px"
      @click="serverAdd"
    >
      {{ LANG.ADD }}
    </fks-button>
    <fks-table :data="mailServerConfig">
      <fks-table-column
        type="index"
        label="#"
        align="center"
        min-width="50px"
      />
      <fks-table-column
        :label="LANG.SERVER_ADDRESS"
        prop="host"
        align="left"
        min-width="120"
        header-align="center"
      />
      <fks-table-column :label="LANG.PORT" prop="port" />
      <fks-table-column
        :label="LANG.ACCOUNT"
        prop="username"
        align="left"
        min-width="120"
        header-align="center"
      />

      <fks-table-column :label="LANG.STATE" prop="state">
        <div slot-scope="scope">
          <fks-tag v-if="scope.row.state" type="success">
            {{ LANG.ON }}
          </fks-tag>
          <fks-tag v-if="!scope.row.state" effect="plain" type="info">
            {{ LANG.OFF }}
          </fks-tag>
        </div>
      </fks-table-column>
      <fks-table-column :label="LANG.OPERATION" width="200px" align="center">
        <span slot-scope="scope">
          <fks-button
            v-if="!scope.row.state"
            v-permission:1="['mail:on']"
            type="text"
            @click="mailOn(scope.$index)"
            >{{ LANG.ENABLE }}</fks-button
          >
          <fks-button
            v-if="scope.row.state"
            v-permission:1="['mail:off']"
            type="text"
            @click="mailOff(scope.$index)"
            >{{ LANG.DISABLE }}</fks-button
          >
          <fks-button
            v-permission:1="['mail:edit']"
            type="text"
            @click="mailEdit(scope.$index)"
            >{{ LANG.EDIT }}</fks-button
          >
          <fks-button
            v-permission:1="['mail:delete']"
            danger-text
            @click="mailCancel(scope.$index)"
            >{{ LANG.DELETE }}</fks-button
          >
        </span>
      </fks-table-column>
    </fks-table>
    <add-config
      :show-dialog="showDialog"
      :config="addConfig"
      :server-type="serverType"
      :server-state="serverState"
      @submit="submit"
      @close="close"
    />
  </div>
</template>

<script>
import AddConfig from './AddConfig'
import { getServerConfig, setServerConfig } from '../../api'
export default {
  name: 'MailServer',
  components: { AddConfig },
  data() {
    return {
      initData: {},
      mailServerConfig: [],
      showDialog: false,
      addConfig: {},
      serverType: 'mailServerConfig',
      serverState: false, //0为编辑，1为新增,
      serverIndex: 0, //当前为第几行,
      deleteConfirm: false
    }
  },
  mounted() {
    this.initList()
  },
  methods: {
    initList() {
      getServerConfig()
        .then((res) => {
          if (res.status) {
            this.mailServerConfig = res.data.mailServerConfig
            this.initData = Object.assign({}, res.data)
          }
        })
        .catch()
    },
    serverAdd() {
      this.serverState = true
      this.showDialog = true
    },
    close() {
      this.showDialog = false
    },
    submit(addConfig, serverState) {
      let newServerConfig = JSON.parse(JSON.stringify(this.mailServerConfig))
      if (this.serverState) {
        newServerConfig.splice(
          newServerConfig.length,
          0,
          JSON.parse(JSON.stringify(addConfig))
        )
      } else {
        newServerConfig.splice(
          this.serverIndex,
          1,
          JSON.parse(JSON.stringify(addConfig))
        )
      }
      this.updateConfig(newServerConfig, serverState)
    },
    updateConfig(newServerConfig, serverState) {
      let params = {
        mailServerConfig: newServerConfig,
        smsServerConfig: this.initData.smsServerConfig,
        fileServerConfig: this.initData.fileServerConfig
      }
      setServerConfig(params).then((res) => {
        if (res.status) {
          if (!this.deleteConfirm) {
            if (serverState) {
              this.$message({
                type: 'success',
                message: this.LANG.ADD_SUCCESS
              })
            } else {
              this.$message({
                type: 'success',
                message: this.LANG.EDIT_SUCCESS
              })
            }
          }
          //关闭弹窗
          this.close()
          this.initList()
        }
      })
    },
    mailOn(index) {
      this.$confirm(this.LANG.MAIL_ON, {
        title: this.LANG.TIPS,
        type: 'warning',
        confirmButtonText: this.LANG.DETERMINE,
        cancelButtonText: this.LANG.CANCEL,
        confirmButtonClass: 'fks-icon-check',
        cancelButtonClass: 'fks-icon-close'
      })
        .then(() => {
          for (var a in this.mailServerConfig) {
            this.mailServerConfig[a].state = false
          }
          this.mailServerConfig[index].state = true
          this.updateConfig(this.mailServerConfig)
        })
        .catch(() => {})
    },
    mailOff(index) {
      let check = false
      this.mailServerConfig.forEach((item) => {
        if (item != this.mailServerConfig[index])
          if (item.state) {
            check = true
          }
      })
      if (check) {
        this.$alert(this.LANG.MAIL_OFF, {
          title: this.LANG.TIPS,
          cancelButtonText: this.LANG.CANCEL,
          type: 'warning',
          confirmButtonText: this.LANG.DETERMINE
        })
          .then(() => {
            this.mailServerConfig[index].state = false
            this.updateConfig(this.mailServerConfig)
          })
          .catch(() => {})
      } else {
        this.$message.warning(this.LANG.MAIL_WARN_OFF)
      }
    },
    mailEdit(index) {
      this.serverState = false
      this.serverIndex = index
      this.showDialog = true
      this.addConfig = JSON.parse(JSON.stringify(this.mailServerConfig[index]))
      this.addConfig.password = ''
    },
    mailCancel(index) {
      if (!this.mailServerConfig[index].state) {
        this.$confirm(this.LANG.MAIL_DELETE, {
          title: this.LANG.TIPS,
          confirmButtonText: this.LANG.DELETE,
          cancelButtonText: this.LANG.CANCEL,
          deleteMode: true
        })
          .then(() => {
            this.$message.success(this.LANG.DELETE_SUCCESS)
            this.deleteConfirm = true
            this.mailServerConfig.splice(index, 1)
            this.updateConfig(this.mailServerConfig)
          })
          .catch(() => {})
      } else {
        this.$message.warning(this.LANG.MAIL_WARN_DELETE)
      }
    }
  }
}
</script>
 